package com.ramos.kuaixiao.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ramos.kuaixiao.entity.FindOrderDetail;

import java.util.ArrayList;
import java.util.List;

public class UnSubmitOrderDB {
    private static String TAG = "UnSubmitOrderDB";
    private static String TABLE_NAME = "UnSubmitOrder";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "id" + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + "DealerId" + " CHAR(30)," + "CustomerId" + " CHAR(30)," + "CustomerName" + " CHAR(30)," +
                "EmployerId" + " CHAR(30)," + "EmployerName" + " CHAR(30)," + "OrderTotalPrice" + " CHAR(30)," + "PayType" + " CHAR(30)," +
                "DeliveryType" + " CHAR(30)," + "OrderStatus" + " CHAR(30)," + "DstAddr" + " CHAR(30)," +
                "Telephone" + " CHAR(30)," + "CommunicatePeople" + " CHAR(30)," + "OrderDate" + " CHAR(30)," + "HouseId" + " CHAR(30)," + "HouseName" + " CHAR(30)," +
                "businessId" + " CHAR(30)," + "businessName" + " CHAR(30)," + "comment" + " CHAR(100)," + "OrderDetailList" + " CHAR(5000));";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    /* 向数据库中添加数据
    * @param name
    * @param age
    * @param sex
    * @param whichclass
    */
    public int addUnSubmitOrder(FindOrderDetail order) {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("insert into UnSubmitOrder(DealerId,CustomerId,CustomerName,EmployerId,EmployerName,OrderTotalPrice,PayType," +
                        "DeliveryType,OrderStatus,DstAddr,Telephone,CommunicatePeople,OrderDate,HouseId,HouseName,businessId,businessName,comment,OrderDetailList) " +
                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                new Object[]{order.getDealerId(), order.getCustmerId(), order.getCustmerName(), order.getEmployerId(), order.getEmployerName(), order.getOrderTotalPrice(),
                        order.getPayType(), order.getDeliveryType(), order.getOrderStatus(), order.getDstAddr(), order.getTelephone(), order.getCommunicatePeople(),
                        order.getOrderDate(), order.getHouseId(), order.getHouseName(), order.getBusinessId(), order.getBusinessName(),order.getComment(), order.getOrderDetailList()});
        return 0;
    }

    public int deleteUnSubmitOrder(int id) {
        if (hasUnSubmitOrder(id)) {
            SQLiteDatabase db = DBHelper.open();
            db.execSQL("delete from UnSubmitOrder where id=?", new Object[]{id});
            return 0;
        }
        return -1;
    }

    public boolean hasUnSubmitOrder(int id) {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from UnSubmitOrder where id =? ", new String[]{String.valueOf(id)});
        if (cursor.getCount() > 0) {
            cursor.close();
            Log.d(TAG, "hasCustomer true");
            return true;
        } else {
            Log.d(TAG, "hasCustomer false");
            cursor.close();
            return false;
        }
    }


    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM UnSubmitOrder");
    }

    public List<FindOrderDetail> findAllUnSubmitOrder() {
        List<FindOrderDetail> list = new ArrayList<>();
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from UnSubmitOrder", new String[]{});
        Log.d(TAG, "cursor:" + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            FindOrderDetail orderDetail = new FindOrderDetail();
            orderDetail.setDealerId(cursor.getString(1));
            orderDetail.setCustmerId(cursor.getString(2));
            orderDetail.setCustmerName(cursor.getString(3));
            orderDetail.setEmployerId(cursor.getString(4));
            orderDetail.setEmployerName(cursor.getString(5));
            orderDetail.setOrderTotalPrice(cursor.getString(6));
            orderDetail.setPayType(cursor.getString(7));
            orderDetail.setDeliveryType(cursor.getString(8));
            orderDetail.setOrderStatus(cursor.getString(9));
            orderDetail.setDstAddr(cursor.getString(10));
            orderDetail.setTelephone(cursor.getString(11));
            orderDetail.setCommunicatePeople(cursor.getString(12));
            orderDetail.setOrderDate(cursor.getString(13));
            orderDetail.setHouseId(cursor.getString(14));
            orderDetail.setHouseName(cursor.getString(15));
            orderDetail.setBusinessId(cursor.getString(16));
            orderDetail.setBusinessName(cursor.getString(17));
            orderDetail.setComment(cursor.getString(18));
            orderDetail.setOrderDetailList(cursor.getString(19));
            orderDetail.setId(cursor.getInt(0));
            list.add(orderDetail);
        }
        cursor.close();
        //db.close();
        Log.d(TAG, "findAllUnSubmitOrder:" + list.size());
        return list;
    }
}
